#ifndef ALEIGENVAL_H
#define ALEIGENVAL_H

#include <Eigen/Dense>
#include <alvalue/alvalue.h>


/*
namespace ALEigen{
    enum varType{
    type_a, type_b, type_c
    };

}
*/

AL::ALValue castEigenToAL(const Eigen::MatrixXf & eigMatf){

    int ncols = eigMatf.cols();
    int nrows = eigMatf.rows();
    std::vector<float> stdMatf;
    stdMatf.assign(eigMatf.data(), eigMatf.data()+ncols*nrows);
    AL::ALValue alMatf;
    alMatf.arraySetSize(3);
    alMatf[0] = 0; //vartype, not implemented
    alMatf[1].arraySetSize(3);
    alMatf[1][0] = ncols; //cols
    alMatf[1][1] = nrows; //rows
    alMatf[1][2] = 0; //dont know
    alMatf[2] = AL::ALValue(stdMatf);

    return alMatf;

}

Eigen::MatrixXf castALToEigen(const AL::ALValue & alMatf){

    std::vector<float> stdMatf;
    alMatf[2].ToFloatArray(stdMatf);
    int nCols = (int) alMatf[1][0];
    int nRows = (int) alMatf[1][1];

    Eigen::MatrixXf eigMatf = Eigen::Map<Eigen::MatrixXf>( stdMatf.data(), nRows, nCols );

    return eigMatf;
}

/*
template <class EigenDataType>
AL::ALValue castEigenToAL(const EigenDataType & eigMatf){
    int ncols = eigMatf.cols();
    int nrows = eigMatf.rows();
    std::vector<float> stdMatf;
    stdMatf.assign(eigMatf.data(), eigMatf.data()+ncols*nrows);
    AL::ALValue alMatf;
    alMatf.arraySetSize(3);
    alMatf[0] = 0; //vartype, not implemented
    alMatf[1].arraySetSize(3);
    alMatf[1][0] = ncols; //cols
    alMatf[1][1] = nrows; //rows
    alMatf[1][2] = 0; //dont know
    alMatf[2] = AL::ALValue(stdMatf);

    return alMatf;

}

//Template version of castALToEigen
template <class EigenDataType>
EigenDataType castALToEigen(const AL::ALValue alMatf){
    std::vector<float> stdMatf;
    alMatf[2].ToFloatArray(stdMatf);
    int nCols = (int) alMatf[1][0];
    int nRows = (int) alMatf[1][1];
    //typedef Eigen::Matrix<float,ncols,nrows> custom;
   //Eigen::Map<custom>eigMatf(stdMatf.data());
    EigenDataType eigMatf = Eigen::Map<EigenDataType>( stdMatf.data(), nRows, nCols );

    return eigMatf;

}

*/

#endif //ALEIGENVAL_H
